var exec = cordova.require('cordova/exec');

/**
 * YSPlugin constructor.
 */

var YSPlugin = function() {
    _accessToken = "";
};

YSPlugin.instance = function() {
    return new YSPlugin();
};

/*
 * 初始化萤石的本地代码；回调函数中返回参数为true时代表成功，否则为失败。
 */
YSPlugin.initSDK = function(callback) {
    cordova.exec(function() {
                    console.log("initSDK success!");
                    if (callback != null && callback != 'undefined' && typeof callback == 'function') {
                        callback(true);
                    }
                 }, function() {
                    if (callback != null && callback != 'undefined' && typeof callback == 'function') {
                        console.log("initSDK failed!");
                        callback(false);
                    }
                 }, "YSPlugin", "initSDK", []);
};

/*
 * 登录萤石的账号来获取accessToken,　通过回调函数传出去；为null时则为失败
 */
YSPlugin.prototype.login = function(callback) {
    if (callback != null && callback != 'undefined' && typeof callback == 'function') {
        cordova.exec(function (accessToken) {
                        console.log("login success, accessToken :" + accessToken);
                        this._accessToken = accessToken;
                        callback(accessToken);
                     }.bind(this), function() {
                        callback(null);
                     } , "YSPlugin", "login", []);
    }
};

/*
 *  调用萤石的本地API来显示视频；成功时则跳转显示；否则什么都不做
 */
YSPlugin.prototype.realPlay = function(accessToken, cameraId) {
    cordova.exec(function() { console.log("realPlay success!"); }, function() { console.log("realPlay failed!"); }, "YSPlugin", "realPlay", [accessToken, cameraId]);
};


module.exports = YSPlugin;
